import Vue from "vue";
import App from "./App";
import router from "./router";
import cookie from "js-cookie";
import store from "./store";
import uuidv4 from "uuid/v4";
import "./assets/style/main.css";
import "./assets/style/fill.css";
import "font-awesome/css/font-awesome.min.css";
import platform from "./util/platform";
import api from "./api";
import "mint-ui/lib/style.css";
import {
  Swipe,
  SwipeItem,
  Header,
  Button,
  Switch,
  Cell,
  Lazyload,
  Field,
  Checklist,
  Popup,
  Picker,
  MessageBox,
  Toast,
  Indicator,
  InfiniteScroll,
  Spinner
} from "mint-ui";

import "./mock";
import VueWebp from "vue-webp";

Vue.use(VueWebp);

Vue.use(Lazyload);
Vue.use(InfiniteScroll);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Header.name, Header);
Vue.component(Button.name, Button);
Vue.component(Switch.name, Switch);
Vue.component(Cell.name, Cell);
Vue.component(Field.name, Field);
Vue.component(Checklist.name, Checklist);
Vue.component(Popup.name, Popup);
Vue.component(Picker.name, Picker);
Vue.component(Spinner.name, Spinner);

Vue.prototype.$MessageBox = MessageBox;
Vue.prototype.$Toast = Toast;
Vue.prototype.$Indicator = Indicator;
Vue.prototype.$cookie = cookie;
Vue.prototype.$store = store;
Vue.prototype.$api = api;

Vue.config.productionTip = false;
Vue.prototype.$platform = platform;

store.commit("setInApp", platform.app);

window.Promise.prototype.always = function(callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason =>
      P.resolve(callback()).then(() => {
        throw reason;
      })
  );
};

// 把cookie的token存到store
if (!store.state.token) {
  const authorization = cookie.get("Authorization") || cookie.get("signTicket");
  // todo iOS APP版本更新后把signTicket去掉
  store.commit("setToken", authorization);
}
// 把cookie的uuid存到store
if (!store.state.UUID) {
  let UUID = cookie.get("uuid") || cookie.get("ukey");
  if (!UUID) {
    UUID = uuidv4();
    const domain =
      window.location.host.indexOf("9ji.com") > -1 ? "9ji.com" : "";
    cookie.set("uuid", UUID, { expire: 365, domain });
    // 兼容老版本 todo 改完后去掉老版本兼容
    cookie.set("ukey", UUID, { expire: 365, domain });
  }
  store.commit("setUUID", UUID);
}
// 把cookie的city存到store，如果没有cookie，则定位或ip定位拿到cityid，弹出切换城市
if (!store.state.city) {
  const cookieCity = cookie.get("city");
  let temp = [];
  if (cookieCity) {
    // 53_云南省-5301_昆明市-230102_昆明市区-s_1
    temp = cookieCity.split("-");
  }
  if (temp.length === 4) {
    // 以防APP设错cookie导致页面挂掉
    let cityObject = {
      provinceId: temp[0].split("_")[0],
      provinceName: temp[0].split("_")[1],
      cityId: temp[1].split("_")[0],
      cityName: temp[1].split("_")[1],
      countyId: temp[2].split("_")[0],
      countyName: temp[2].split("_")[1],
      hasShop: !!temp[3].split("_")[1]
    };
    store.commit("setCity", cityObject);
  } else {
    if (cookie.get("cityid")) {
      // 删掉cityid，重新设置
      const domain =
        window.location.host.indexOf("9ji.com") > -1 ? "9ji.com" : "";
      cookie.remove("cityid", { expire: 365, domain });
    }
  }
}

function setRootEm() {
  /* eslint-disable*/
  var cw = window.innerWidth;
  cw = cw / 10;
  //计算倍数，数值可变。
  if (cw < 32) {
    cw = 32;
  } //最小宽度
  if (cw > 64) {
    cw = 64;
  } //最大宽度
  document.documentElement.style.cssText = "font-size:" + cw + "px !important";
}
function setViweport() {
  document
    .querySelector("meta[name=viewport]")
    .setAttribute(
      "content",
      "width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
    );
}

setRootEm();
setViweport();
window.onresize = function() {
  setRootEm();
};
if (window.location.href.indexOf("mdev=true") > -1) {
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript");
  script.setAttribute("src", "//cdn.jsdelivr.net/npm/eruda");
  var head = document.getElementsByTagName("head")[0];
  head.appendChild(script);
  script.onload = function() {
    eruda.init();
  };
}

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  template: "<App/>",
  components: { App }
});
